import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'
import Users from './components/Users.vue'
import Rights from './components/Rights.vue'
import Categories from './components/Categories.vue'
import Goods from './components/Goods.vue'
import Orders from './components/Orders.vue'
import Reports from './components/Reports.vue'
import Roles from './components/Roles.vue'
import Params from './components/Params.vue'
import Add from './components/Add.vue'
Vue.use(Router)

const router = new Router({
  routes: [
    // {
    //   path: '/',
    //   name: 'home',
    //   component: Home
    // },
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
    // }
    {path:"/",redirect:"/login"},
    {path:'/login',component:Login},
    {
      path:'/home',
      component:Home,
      redirect:'/welcome',
      children:[
        {path:'/welcome',component:Welcome},
        {path:'/users',component:Users},
        {path:'/rights',component:Rights},
        {path:'/categories',component:Categories,},
        {path:'/goods',component:Goods},
        {path:'/orders',component:Orders},
        {path:'/reports',component:Reports},
        {path:'/roles',component:Roles},
        {path:'/params',component:Params},
        {path:'/goods/add',component:Add}
      ]
    }
  ]
})

// 路由导航守卫 必须登录才能看别的页面
router.beforeEach((to,from,next) => {
  // to 要去的页面  from 从哪个页面来 next 放行
  if(to.path == '/login') return next();
  let tokenStr = window.sessionStorage.getItem('token');
  if(!tokenStr) return next('/login');
  next();
})

export default router
